package com.yanzhengwu.boot.core.druid.properties;

import com.alibaba.druid.pool.DruidDataSource;
import com.yanzhengwu.boot.core.common.util.DynamicAssert;
import lombok.Data;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.security.InvalidParameterException;
import java.util.Map;


/**
 * 数据源配置文件
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "spring.datasource.druid")
public class DynamicDataSourceProperties {

    private int initialSize;

    private int minIdle;

    private int maxActive;

    private int maxWait;

    private int timeBetweenEvictionRunsMillis;

    private int minEvictableIdleTimeMillis;

    private int maxEvictableIdleTimeMillis;

    private String validationQuery;

    private Boolean testWhileIdle;

    private Boolean testOnBorrow;

    private Boolean testOnReturn;

    private String passwordCallback;

    private String connectionProperties;
    private String dbName;

    private String url;
    private String username;
    private String password;
    private String publicKey;
    private Boolean encryptEnable;
    private String driverClassName;
    private String filters;

    private Map<Object,Object> webStatFilter;
    private Map<Object,Object> statViewServlet;
    private Map<Object,Object> filter;

    public DruidDataSource setDataSource(DruidDataSource datasource) throws Exception {
        if (encryptEnable) {
            DynamicAssert.connectionPropertiesCheck(connectionProperties);
            DynamicAssert.passwordCallbackCheck(passwordCallback);
        }

        datasource.setName(dbName);
        datasource.setUrl(url);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);

        datasource.setConnectionProperties(this.connectionProperties + "password=" + password + ";");

        datasource.setPasswordCallbackClassName(passwordCallback);
        datasource.setInitialSize(initialSize);
        /** 配置初始化大小、最小、最大 */
        datasource.setInitialSize(initialSize);
        datasource.setMaxActive(maxActive);
        datasource.setMinIdle(minIdle);
        /** 配置获取连接等待超时的时间 */
        datasource.setMaxWait(maxWait);
        /** 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒 */
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        /** 配置一个连接在池中最小、最大生存的时间，单位是毫秒 */
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setMaxEvictableIdleTimeMillis(maxEvictableIdleTimeMillis);
        /**
         * 用来检测连接是否有效的sql，要求是一个查询语句，常用select 'x'。如果validationQuery为null，testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
         */
        datasource.setValidationQuery(validationQuery);
        /** 建议配置为true，不影响性能，并且保证安全性。申请连接的时候检测，如果空闲时间大于timeBetweenEvictionRunsMillis，执行validationQuery检测连接是否有效。 */
        datasource.setTestWhileIdle(testWhileIdle);
        /** 申请连接时执行validationQuery检测连接是否有效，做了这个配置会降低性能。 */
        datasource.setTestOnBorrow(testOnBorrow);
        /** 归还连接时执行validationQuery检测连接是否有效，做了这个配置会降低性能。 */
        datasource.setTestOnReturn(testOnReturn);

        datasource.setFilters(filters);

        return datasource;
    }


}

